package com.cci.kangdao.service.impl;

import com.cci.kangdao.aop.UserThreadLocal;
import com.cci.kangdao.common.type.NotifyModelType;
import com.cci.kangdao.dao.CommonNotifyRecordTDao;
import com.cci.kangdao.dao.model.CommonNotifyRecordT;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dto.PagedResponse;
import com.cci.kangdao.dto.response.GetFieldServiceNotifyOrderTResponse;
import com.cci.kangdao.escalation.dto.response.GetNotifyListResponse;
import com.cci.kangdao.escalation.dto.response.GetNotifyResponse;
import com.cci.kangdao.fieldService.dao.FieldServiceWorkOrderDao;
import com.cci.kangdao.fieldService.model.FieldServiceWorkOrder;
import com.cci.kangdao.service.CommonNotifyRecordService;
import com.cci.kangdao.service.DictionariesTService;
import com.cci.kangdao.utilTool.BeanUtils;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.LocalTimeUtil;
import com.cci.kangdao.utilTool.ParameterTool;
import com.cci.kangdao.utilTool.RequestContextHolder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author William Du
 * @date 2020/8/3
 */
@Service
public class CommonNotifyRecordServiceImpl implements CommonNotifyRecordService {

    @Resource
    private CommonNotifyRecordTDao commonNotifyRecordTDao;

    @Resource
    private DictionariesTService dictionariesTService;

    @Resource
    private FieldServiceWorkOrderDao fieldServiceWorkOrderDao;

    @Override
    public void getPagedOrderListByNotifyCategory(Map<String, Object> map) {
        Integer page = MapUtils.getInteger(map, "page", 1);
        Integer pageSize = MapUtils.getInteger(map, "pageSize", 10);
        Integer category = MapUtils.getInteger(map, "category", 0);
        UserT userT = UserThreadLocal.get();
        if (category.equals(1)) {

        } else if (category.equals(2)) {
            PageHelper.startPage(page, pageSize, true);
            List<Map<String, Object>> orderList = commonNotifyRecordTDao.selectCBUOrderListByNotify(userT.getId(), NotifyModelType.CFWO.getCode());
            PageInfo info = new PageInfo<>(orderList);
            PagedResponse<GetFieldServiceNotifyOrderTResponse> response = new PagedResponse<>();
            response.setTotal(info.getTotal());
            List<GetFieldServiceNotifyOrderTResponse> resultList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(orderList)) {
                List<Map<String, Object>> statusList = dictionariesTService.getDictionaryList("FieldOrderStatus");
                Map<String, String> statusMap = new HashMap<>();
                if (CollectionUtils.isNotEmpty(statusList)) {
                    for (Map<String, Object> sMap : statusList) {
                        Integer dInt = MapUtils.getInteger(sMap, "DInt");
                        String dValue = MapUtils.getString(sMap, "DValue");
                        statusMap.put(String.valueOf(dInt), dValue);
                    }
                }
                for (Object orderObj : info.getList()) {
                    GetFieldServiceNotifyOrderTResponse notifyOrderT = BeanUtils.convert(orderObj, GetFieldServiceNotifyOrderTResponse.class);
                    String statusStr = String.valueOf(notifyOrderT.getStatus());
                    notifyOrderT.setStatusValue(MapUtils.getString(statusMap, statusStr, ""));
                    resultList.add(notifyOrderT);
                }
            }
            response.setList(resultList);
            ParameterTool.writeSuccessResponse(RequestContextHolder.getResponse(), response);
        }
    }

    @Override
    public void getNotifyList(Map<String, Object> map) {
        String category = MapUtils.getString(map, "category");
        Long orderId = MapUtils.getLong(map, "orderId");
        UserT userT = UserThreadLocal.get();
        if (category.equals("1")) {

        } else if (category.equals("2")) {
            List<CommonNotifyRecordT> recordTS = commonNotifyRecordTDao.selectListByOrderId(NotifyModelType.CFWO.getCode(), userT.getId(), orderId);
            GetNotifyListResponse response = new GetNotifyListResponse();
            List<GetNotifyResponse> notifyResponseList = BeanUtils.convertList(recordTS, GetNotifyResponse.class);
            response.setNotifyList(notifyResponseList);
            ParameterTool.writeSuccessResponse(RequestContextHolder.getResponse(), response);
            // 设置已读该工单通知
            commonNotifyRecordTDao.updatePushReadStatusByReceiverId(NotifyModelType.CFWO.getCode(), orderId, userT.getId(), LocalTimeUtil.getRealCurrentTime());
        }

    }

}
